#!/usr/bin/env python2

from benchmarker import *
import re
import os


class Log:
	def __init__(self, content):
		self.content = content

		self.phases = {}

		p = re.compile(r'\<START:(?P<value>[^:]*)\>(.*)\<END:(?P=value)\>', re.DOTALL)
		m = p.findall(self.content)

		for phase in m:
			self.phases[phase[0]] = phase[1]


	def isValid(self):
		if 'benchmark' not in self.phases.keys():
			return False
		jvm = re.sub(r'(?P<log>\<[^:]*:[^:]*\>)', '', self.phases['benchmark']).rstrip()
		if jvm:
			return False

		return True

	def getResult(self):
		if(self.isValid() == False):
#			raise DirtyResult('JVM')
			return 'dirty'

		ret = self.getLogValue('RESULT', self.phases['benchmark'])
		if(len(ret) == 1):
			return ret[0]

		return 'multiple results'

	def getLogValue(self, label, log):
		p = re.compile(r'\<' + label + ':(?P<value>[^:]*)\>', re.DOTALL)
		m = p.findall(log)
		return m


	def __str__(self):
		display = ''
		for k in self.phases.keys():
			display += k + '\n'
			display += '\t' + self.phases[k] + '\n'
#		return str(self.phases)
		return display


def main():
	with open('tmp.output') as f:
		linestring = f.read()
#		print(linestring)
#		content = f.readlines()
		l = Log(linestring)
		print(l)
		print(l.isValid())
		print(l.getResult())


if __name__ == '__main__':
	main()
					
